/**
 * 
 */
package GUI;

import java.awt.LayoutManager;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;

import Entity.Event;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * @author aviat
 *
 */
public class ChooseEvent extends JPanel {

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JButton btnRefresh;
	private JButton btnContinue;
	private JLabel lblChooseEvent;
	private JLabel CustomerIDiconlabel;
	private JComboBox<String> groom_bride_comboBox;
	private JComboBox<String> EventDatecomboBox;
	private JComboBox<String> customerIDcomboBox;
	
	private ArrayList<Event> e_allDate;
	private ArrayList<Event> e_filteredDate;
	private Object syncObject;
	private boolean e_isChangeAllowed;
	private String place;
	private Event event;
	/**
	 * Constructor
	 */
	public ChooseEvent() {
		setLayout(null);
		btnRefresh = new JButton("Refresh");
		btnRefresh.setIcon(new ImageIcon(ChooseEvent.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(null,"ALL");
				}
			}
		});
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ChooseEvent.class.getResource("/resources/back22.png")));
		btnContinue = new JButton("Continue");
		btnContinue.setIcon(new ImageIcon(ChooseEvent.class.getResource("/resources/continueArrow.png")));
		groom_bride_comboBox = new JComboBox<String>();
		groom_bride_comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(groom_bride_comboBox.getSelectedItem().toString(),"G_B_NAMES");
				}
			}
		});
		EventDatecomboBox = new JComboBox<String>();
		EventDatecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(EventDatecomboBox.getSelectedItem().toString(),"EDATE");
					}
				}
			}
		});
		customerIDcomboBox = new JComboBox<String>();
		customerIDcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(customerIDcomboBox.getSelectedItem().toString(),"CUSTOMERID");
				}
			}
		});
		
		initialize();
		}


		private void initialize() {
			
			JLabel lblCustmerName = new JLabel("Groom & Bride :");
			lblCustmerName.setForeground(Color.WHITE);
			lblCustmerName.setFont(new Font("Tahoma", Font.BOLD, 12));
			lblCustmerName.setBounds(134, 220, 101, 27);
			add(lblCustmerName);
			
			btnContinue.setFont(new Font("Tahoma", Font.BOLD, 12));
			
			JLabel lblEventDate = new JLabel("Event Date: ");
			lblEventDate.setForeground(Color.WHITE);
			lblEventDate.setFont(new Font("Tahoma", Font.BOLD, 12));
			lblEventDate.setBounds(134, 145, 101, 27);
			add(lblEventDate);
			
			btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
			btnBack.setBounds(10, 462, 101, 27);
			add(btnBack);
			
			btnContinue.setBounds(263, 369, 134, 27);
			add(btnContinue);
			
			lblChooseEvent = new JLabel("Choose Event");
			lblChooseEvent.setForeground(new Color(0, 204, 255));
			lblChooseEvent.setFont(new Font("Tahoma", Font.BOLD, 16));
			lblChooseEvent.setBounds(10, 11, 126, 27);
			add(lblChooseEvent);
			
			
			ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
			ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
			
			
			groom_bride_comboBox.setEditable(true);
			groom_bride_comboBox.setBounds(263, 225, 134, 27);
			AutoCompletion.enable(this.groom_bride_comboBox);
			add(groom_bride_comboBox);
			
			
			EventDatecomboBox.setEditable(true);
			EventDatecomboBox.setBounds(263, 146, 134, 27);
			AutoCompletion.enable(this.EventDatecomboBox);
			add(EventDatecomboBox);
			
			JLabel lblCustomerId = new JLabel("Customer ID:");
			lblCustomerId.setForeground(Color.WHITE);
			lblCustomerId.setFont(new Font("Tahoma", Font.BOLD, 12));
			lblCustomerId.setBounds(134, 299, 101, 27);
			add(lblCustomerId);
			
			customerIDcomboBox.setEditable(true);
			customerIDcomboBox.setBounds(263, 300, 134, 27);
			AutoCompletion.enable(this.groom_bride_comboBox);
			add(customerIDcomboBox);
			
			btnRefresh.setBounds(439, 224, 117, 27);
			add(btnRefresh);
			
			CustomerIDiconlabel = new JLabel("Customer ID:");
			CustomerIDiconlabel.setIcon(LOGO);
			CustomerIDiconlabel.setBounds(0, 0, 786, 500);
			add(CustomerIDiconlabel);
		}

		public void setAllEvents(ArrayList<Event> list){
			e_allDate = list;
			e_filteredDate = new ArrayList<>();
			syncObject = new Object();
			event = new Event();
			filterList(null,"ALL");
			 
		}
	/**
	 * This method handle the comboboxes of the class to filter the chosen item end fill all
	 * the other combobxes with the suitable values
	 * 
	 * @param selectedItem is the selected item from the combobox
	 * @param scope is to know where to go in the switch-case
	 */
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			e_filteredDate.clear();
			
			switch(scope)
			{
			case "EDATE":
				for (Event e :e_allDate) 
				{
					if(e.getEvetDate().equals(selectedItem))
					{
						e_filteredDate.add(e);
					}
				}
				break;
			case "G_B_NAMES":
				for (Event e :e_allDate) 
				{
					if(e.getG_b_names().equals(selectedItem))
					{
						e_filteredDate.add(e);
					}
				}
				break;
			case "CUSTOMERID":
				for (Event e :e_allDate) 
				{
					if(String.valueOf(e.getCustomerID()).equals(selectedItem))
					{
						e_filteredDate.add(e);
					}
				}
				break;
			case "ALL":
				System.out.println("in all");
				e_filteredDate.addAll(e_allDate);
				break;
			}
		}
	
		e_isChangeAllowed = false;
		filterComboBox(EventDatecomboBox,"EDATE");
		filterComboBox(groom_bride_comboBox,"G_B_NAMES");
		filterComboBox(customerIDcomboBox,"CUSTOMERID");
		if(EventDatecomboBox.getItemCount()==1 && groom_bride_comboBox.getItemCount()==1 && customerIDcomboBox.getItemCount()==1)
		{
			for(Event e: e_filteredDate)
			{	
				if(e.getCustomerID()==Integer.parseInt(customerIDcomboBox.getSelectedItem().toString()))
				{
					place=e.getEvetPlace();
				}
			}
		}
		e_isChangeAllowed = true;

	}
		/**
		 * @return the event
		 */
		public Event getEvent() {
			return event;
		}


		/**
		* This method adding items to combobox
		* @param combo is the combobox to add item to
		* @param scope is the key code for adding the correct field 
		* 
		*/
		private void filterComboBox(JComboBox<String> combo, String scope)
		{
			combo.removeAllItems();
			synchronized (syncObject)
			{
				for(Event e: e_filteredDate )
				{
					switch(scope)
					{
					case "EDATE":
						combo.addItem(e.getEvetDate());
						break;
					case "G_B_NAMES":
						combo.addItem(e.getG_b_names());
						break;
					case "CUSTOMERID":
						combo.addItem(String.valueOf(e.getCustomerID()));
						break;
					}
				}
			}
		}
		/**
		* @return the e_isChangeAllowed
		* 
		*/
		private boolean isChangeAllowed()
		{
			return e_isChangeAllowed;
		}
		/**
		* @return the btnBack
		* 
		*/
		public JButton getBtnBack() {
				return btnBack;
		}
		/**
		 * @return the btnContinue
		 */
		public JButton getBtnContinue() {
			return btnContinue;
		}
			
		/**
		 * @return the customerIDcomboBox
		 */
		public JComboBox<String> getCustomerIDcomboBox() {
			return customerIDcomboBox;
		}
	
		/**
		 * @param customerIDcomboBox the customerIDcomboBox to set
		 */
		public void setCustomerIDcomboBox(JComboBox<String> customerIDcomboBox) {
			customerIDcomboBox = customerIDcomboBox;
		}
	
		/**
		 * @return the eventDatecomboBox
		 */
		public JComboBox<String> getEventDatecomboBox() {
			return EventDatecomboBox;
		}
	
		/**
		 * @param eventDatecomboBox the eventDatecomboBox to set
		 */
		public void setEventDatecomboBox(JComboBox<String> eventDatecomboBox) {
			EventDatecomboBox = eventDatecomboBox;
		}
	
		/**
		 * @param btnBack the btnBack to set
		 */
		public void setBtnBack(JButton btnBack) {
			this.btnBack = btnBack;
		}
	
		/**
		 * @param btnContinue the btnContinue to set
		 */
		public void setBtnContinue(JButton btnContinue) {
			this.btnContinue = btnContinue;
		}
	
	
		/**
		 * @return the groom_bride_comboBox
		 */
		public JComboBox<String> getGroom_bride_comboBox() {
			return groom_bride_comboBox;
		}
	
	
		/**
		 * @param groom_bride_comboBox the groom_bride_comboBox to set
		 */
		public void setGroom_bride_comboBox(JComboBox<String> groom_bride_comboBox) {
			this.groom_bride_comboBox = groom_bride_comboBox;
		}


		/**
		 * @return the btnRefresh
		 */
		public JButton getBtnRefresh() {
			return btnRefresh;
		}


		/**
		 * @return the place
		 */
		public String getPlace() {
			return place;
		}


		/**
		 * @param place the place to set
		 */
		public void setPlace(String place) {
			this.place = place;
		}
}
